﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manager_ListAdmin : System.Web.UI.Page
{
    CommonClass ccobj = new CommonClass();
    DBClass dbobj = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindgrid();
    }
    protected void bindgrid()
    {
        string strsql = "select * from Admin";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabAdmin");

        if (ViewState["SortDirection"] == null)
            GridView1.DataSource = dstable.DefaultView;
        else
        {
            DataView sortedDV = new DataView(dstable);
            sortedDV.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();
            GridView1.DataSource = sortedDV;
        }
        GridView1.DataKeyNames = new string[] { "AdminID" };
        GridView1.DataBind();
    }
    public string getDepartName(string DepartID)
    {
        string strsql = "select DepartmentName from Department where DepartmentID='" + DepartID + "'";
        return dbobj.ExecScalar(strsql);
    }
    public string Isassess(int isassess)
    {
        string str = "";
        if (isassess == 0) str = "否";
        else str = "是";
        return str;
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
            Response.Write(ccobj.MessageBoxPage("请输入关键字！"));
        else
        {
            string strsql = "select * from Admin";
            switch (DropDownList1.SelectedItem.Text.Trim())
            {
                case "工号":
                    strsql += " where AdminID like '%" + TextBox1.Text.Trim() + "%'";
                    break;
                case "部门":
                    string strID = dbobj.ExecScalar("select DepartmentID from Department where DepartmentName like '%" + TextBox1.Text.Trim() + "%'");
                    if (strID != "") strsql += " where DepartmentID='" + strID + "'";
                    break;
            }
            DataTable dstable = dbobj.GetDataSet(strsql, "tabadmin");
            GridView1.DataSource = dstable.DefaultView;
            GridView1.DataKeyNames = new string[] { "AdminID" };
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        string str = GridView1.DataKeys[e.RowIndex].Value.ToString().Trim();

        if (Session["UserID"].ToString() != str)
        {
            string strsql = "delete from Admin where AdminID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            dbobj.ExecNonQuery(strsql);
            Response.Write(ccobj.MessageBox("删除成功！"));
            bindgrid();
        }
        else
        {
            Response.Write(ccobj.MessageBox("您不能删除自己！"));
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
            Response.Redirect("../Manager/EditAdmin.aspx?id=" + e.CommandArgument);
    }
    protected void GridView1_Sorting1(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "DESC";
        if (ViewState["SortDirection"].ToString() == "ASC")
            ViewState["SortDirection"] = "DESC";
        else
            ViewState["SortDirection"] = "ASC";
        ViewState["SortExpression"] = e.SortExpression;
        bindgrid();
    }
}